define([
        'app',
        './API'
    ],
    function (app) {
        'use strict';
        app.factory('OauthAPI', ['$rootScope', '$localForage', '$resource', '$interval',
            function ($rootScope, $localForage, $resource, $interval) {
                var _oahthServer = 'http://oauth.m-lab.cn';

                function _queryStringParse(str) {
                    if (typeof str !== 'string')
                        return {};
                    str = str.trim().replace(/^(\?|#|&)/, '');
                    if (!str)
                        return {};
                    return str.split('&').reduce(function (ret, param) {
                        var parts = param.replace(/\+/g, ' ').split('=');
                        var key = parts.shift();
                        var val = parts.length > 0 ? parts.join('=') : undefined;
                        key = decodeURIComponent(key);
                        val = val === undefined ? null : decodeURIComponent(val);
                        if (!ret.hasOwnProperty(key)) {
                            ret[key] = val;
                        } else if (Array.isArray(ret[key])) {
                            ret[key].push(val);
                        } else {
                            ret[key] = [ret[key], val];
                        }
                        return ret;
                    }, {});
                }

                return {
                    weibo: {
                        get_authorize_link: function (callbackUrl, state, isMobile, isEnglish) {
                            var redirect_uri = _oahthServer + '/weibo/callback' + '?callback=' + callbackUrl;
                            var link = 'https://api.weibo.com/oauth2/authorize?client_id=1312616958';
                            link += '&redirect_uri=' + encodeURIComponent(redirect_uri);
                            link += '&scope=follow_app_official_microblog';
                            if (isMobile)
                                link += '&display=mobile';
                            if (isEnglish)
                                link += '&language=en';
                            if (state)
                                link += '&state=' + state;
                            link += '&response_type=code';
                            return link;
                        }
                    },
                    qq: {
                        get_authorize_link: function (callbackUrl, state, isMobile) {
                            var redirect_uri = _oahthServer + '/qq/callback' + '?callback=' + callbackUrl;
                            var link = 'https://graph.qq.com/oauth2.0/authorize?client_id=101001251';
                            link += '&redirect_uri=' + encodeURIComponent(redirect_uri);
                            link += '&scope=get_user_info';
                            if (isMobile)
                                link += '&display=mobile';
                            if (state)
                                link += '&state=' + state;
                            link += '&response_type=code';
                            return link;
                        }
                    },
                    weixin: {
                        get_authorize_link: function (callbackUrl, state) {
                            var redirect_uri = _oahthServer + '/weixin/callback?callback=' + callbackUrl;
                            var link = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx763742d9bf7b1b13';
                            link += '&redirect_uri=' + encodeURIComponent(redirect_uri);
                            link += '&response_type=code';
                            link += '&scope=snsapi_userinfo';
                            if (state)
                                link += '&state=' + state;
                            link += '#wechat_redirect';
                            return link;
                        },
                        get_authorize_base_link: function (callbackUrl, state) {
                            var redirect_uri = _oahthServer + '/weixin/callback?callback=' + callbackUrl;
                            var link = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx763742d9bf7b1b13';
                            link += '&redirect_uri=' + encodeURIComponent(redirect_uri);
                            link += '&response_type=code';
                            link += '&scope=snsapi_base';
                            if (state)
                                link += '&state=' + state;
                            link += '#wechat_redirect';
                            return link;
                        }
                    },
                    oauth: function (link) {
                        window.location = link;
                    },
                    oauthInframe: function (path, link, container, callback) {
                        var iframe = document.createElement("iframe");
                        iframe.onload = function () {
                            $rootScope.hideLoading();
                        };
                        iframe.style.width = '100%';
                        iframe.style.height = '100%';
                        iframe.src = link;
                        document.getElementById(container).innerHTML = '';
                        document.getElementById(container).appendChild(iframe);
                        if (callback)
                            var _myInterval = $interval(function () {
                                var url;
                                try {
                                    url = iframe.contentWindow.location.href;
                                } catch (e) {
                                }
                                if (url && callback && url.indexOf(path) !== -1) {
                                    callback(_queryStringParse(url.split(path + '?')[1]));
                                    callback = null;
                                    $interval.cancel(_myInterval);
                                }
                            }, 500);
                    }
                };
            }
        ]);
    }
);
